package com.atguigu.test.juctest.ThreadCommunication;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 实现线程间的定制化通信(多线程条件下达到精确通知)
 * a:10
 * b:15
 * c20
 */
class ShareResource{
    volatile int number=1; //a=1 b=2 c=3
    Lock lock=new ReentrantLock();
    Condition condition1=lock.newCondition();
    Condition condition2=lock.newCondition();
    Condition condition3=lock.newCondition();

    public void print10() throws InterruptedException {
              lock.lock();//拿锁

                      try {
                         //判断
                          while (number!=1){
                              //如果不是1，1就等待
                              condition1.await();
                          }
                            //是1,1就干活
                          for (int i = 1; i <= 10; i++) {
                              System.out.println(Thread.currentThread().getName()+"\t"+"我先干活了啊"+i);
                          }
                          //通知2
                        number=2;
                          condition2.signal();
                      } finally {
                          //释放锁
                          lock.unlock();
                      }
    }

    /**
     * 打印15次
     */
    public void print15() throws InterruptedException {
              lock.lock();//拿锁
                      try {
                         //判断
                          while (number!=2){
                              //2等待
                              condition2.await();
                          }
                          //否则是2
                          //干活
                          for (int i = 1; i <= 15; i++) {
                              System.out.println(Thread.currentThread().getName()+"\t"+"我接着干了哈"+i);
                          }
                         //通知
                          number=3;
                          condition3.signal();
                      } finally {
                          //释放锁
                          lock.unlock();
                      }


    }

    /**
     * 打印20次
     */
    public void print20() throws InterruptedException {
              lock.lock();//拿锁
                      try {
                       //判断
                       while (number!=3){
                           //如果不是3,3等待,直到是它才干活
                           condition3.await();
                       }
                        //是3
                          //干活
                          for (int i = 1; i <= 20; i++) {
                              System.out.println(Thread.currentThread().getName()+"\t"+"我是c我也干了哈"+i);
                          }
                          //通知
                          number=3;
                          condition3.signal();
                      } finally {
                          //释放锁
                          lock.unlock();
                      }
    }
}
public class ThreadOrderAccess {
    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();
        //线程操作资源类
        new Thread(() -> {
            try {
                shareResource.print10();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A").start();

        new Thread(() -> {
            try {
                shareResource.print15();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "B").start();

        new Thread(() -> {
            try {
                shareResource.print20();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "C").start();
    }
}
